///<reference path="bluelotus7.common.js" />
var currentDomainScopeType, currentDomainScopeKeyword;

function queryDomainScopeByCriterias(container) {
    if (!container) {
        container = $("#queryPanel");
    }

    currentDomainScopeType = $("#domainScopeTypeList").val();
    currentDomainScopeKeyword = $("#domainScopeKeyword").val();

    refreshDomainScopeGrid();
}

function refreshDomainScopeGrid(container) {
    if (!container) {
        container = $("#queryResult");
    }

    var maskField = bluelotus7.mask(container, "Retrieving data...");
    var postData = {
        Type: currentDomainScopeType,
        Keyword: currentDomainScopeKeyword
    };

    $.ajax({
        url: appendRandom(ajaxCollection["QueryDomainScope"]),
        type: 'POST',
        async: false,
        data: JSON.stringify(postData),
        contentType: 'application/json',
        //dataType: 'json',
        success: function (result) {
            if (result) {
                container.children().remove();
                container.append(result);
            }

            bluelotus7.unmask(maskField);
        }
    });
}
//used for domain scope info view & management
function initBtnForDomainScope() {
    var container = $("div.domainScopePanel");
    var actionPanel = container.find(".actionPanel");
    formUtil.switchButton(actionPanel, true);

    actionPanel.find(".btn-cancel").bind("click", { container: container, actionPanel: actionPanel }, function (e) {
        formUtil.setToReadonly(e.data.container);
        formUtil.switchButton(e.data.actionPanel, true);
    });

    actionPanel.find(".btn-edit").bind("click", { container: container, actionPanel: actionPanel }, function (e) {
        formUtil.setToEdit(e.data.container);
        formUtil.switchButton(e.data.actionPanel, false);
    });

    actionPanel.find(".btn-submit").bind("click", { container: container, actionPanel: actionPanel }, function (e) {
        var thisRef = e.data.container;
        var data = formUtil.getData(thisRef);
        var maskField = bluelotus7.mask(thisRef, "Sync...");
        $.ajax({
            url: appendRandom(ajaxCollection["AddOrUpdateDomainScope"]),
            type: 'POST',
            async: false,
            data: JSON.stringify(data),
            contentType: 'application/json',
            dataType: 'json',
            success: function (result) {
                if (result) {
                    if (result.IsSuccess) {
                        formUtil.applyNewData(thisRef);
                        formUtil.setToReadonly(thisRef);
                        formUtil.switchButton(thisRef, true);
                    } else if (result.ErrorMessage) {
                        alert(result.ErrorMessage);
                    }
                }

                bluelotus7.unmask(maskField);
            }
        });
    });
}
//used for domain scope user info view & management
function initBtnForDomainScopeUser() {
    var container = $("div.domainScopeUserPanel");
    var actionPanel = container.find(".actionPanel");
    formUtil.switchButton(actionPanel, true);

    actionPanel.find(".btn-cancel").bind("click", { container: container, actionPanel: actionPanel }, function (e) {
        formUtil.setToReadonly(e.data.container);
        formUtil.switchButton(e.data.actionPanel, true);
    });

    actionPanel.find(".btn-edit").bind("click", { container: container, actionPanel: actionPanel }, function (e) {
        formUtil.setToEdit(e.data.container);
        formUtil.switchButton(e.data.actionPanel, false);
    });
}

//used for IM users view & management
function initBtnForIMUsers(container) {
    if (!container) {
        container = $("div.imUsers");
    }

    var actionPanel = container.find("div.actionPanel");
    actionPanel.find(".btn-cancel").bind("click", { container: container, actionPanel: actionPanel }, function (e) {
        var table = e.data.container.find("table.imUserGrid");
        table.children("tbody").children("tr").each(function (i, c) {
            var needMonitorCell = $(this).children("td.need-monitor");
            var original = needMonitorCell.attr("value") == "true" ? true : false;
            needMonitorCell.find("input:checkbox").attr("checked", original);
        });
    });

    actionPanel.find(".btn-refresh").bind("click", { container: container, actionPanel: actionPanel }, function (e) {
        window.location.reload();
    });

    actionPanel.find(".btn-submit").bind("click", { container: container, actionPanel: actionPanel }, function (e) {
        var thisRef = e.data.container;
        var table = e.data.container.find("table.imUserGrid");
        var toUpdate = new Array();
        table.children("tbody").children("tr").each(function (i, c) {
            var needMonitorCell = $(this).children("td.need-monitor");
            var original = needMonitorCell.attr("value") == "true" ? true : false;
            var newValue = needMonitorCell.find("input:checkbox").attr("checked") ? true : false;
            if (original ^ newValue) {
                toUpdate.push({ Key: $(this).children(".identity").text(), NeedMonitor: newValue ? true : false });
            }
        });

        if (toUpdate.length > 0) {
            var maskField = bluelotus7.mask(thisRef, "Sync...");
            $.ajax({
                url: appendRandom(ajaxCollection["UpdateIMNeedMonitor"]),
                type: 'POST',
                async: false,
                data: JSON.stringify({ Items: toUpdate }),
                contentType: 'application/json',
                dataType: 'json',
                success: function (result) {
                    if (result) {
                        if (result.IsSuccess) {
                            alert("Updated successfully!");
                        } else if (result.ErrorMessage) {
                            alert(result.ErrorMessage);
                        }
                    }

                    bluelotus7.unmask(maskField);
                }
            });
        } else {
            alert("No data changed");
        }
    });

}

function addOrUpdateDomainScope(container) {
    if (!container) {
        container = $("div.domainScopePanel");
    }
    if (container) {
    }
}